<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class UploadController extends Controller
	{
		//上传头像/logo
		public function headimg($token=null, $uid=null, $type=null)
		{
			$userinfo = M("User") -> where("uid = {$_POST['uid']}") ->find();
			/* if ($userinfo['token'] != $_POST['token'])
			{
				$array['result'] = -2;
				$array['message'] = "token过期或错误，请重新登录获取";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			} */
			$img = base64_decode($_REQUEST['headimg']);		
			$oldname = $_REQUEST['oldname'];
			$index = strrpos($oldname, ".");//对文件名进行“.”截取
			$ext = substr($oldname, $index);//获得文件的扩展名
			$newname = md5(time() . rand(0, 9999)).$ext;
			$result = file_put_contents('./Public/Uploads/Headimg/'.$newname, $img);
			$data["headimg"] = "http://192.168.1.119/loveshow/Public/Uploads/Headimg/".$newname;
			
			$data['uid'] = $uid;
			if ($_POST['type'] == 0)
			{
				$result = M("Userinfo") -> where("uid={$_POST['uid']}") -> find();
				if ($result != null)
				{
					
					//更改头像
					if ($result["headimg"] == null) 
					{
						//该用户已完善资料，但未上传头像
						$id = M("Userinfo") -> where("uid={$_POST['uid']}") -> setField('headimg',$data['headimg']);
					}
					else
					{
						//该用户已完善过资料且上传了头像
// 						$headimgName = substr($result["headimg"], -36);
						$headimgArray = explode("/", $result["headimg"]);
// 						$headimgName = substr($result["headimg"], -36);
						$headimgName = end($headimgArray);
						$delResult = unlink("./Public/Uploads/Headimg/".$headimgName);
						$id = M("Userinfo") -> where("uid={$_POST['uid']}") -> setField('headimg',$data['headimg']);
					}
				}
				else
				{
					//上传头像
					$id = M("Userinfo") -> add($data);
				}
			}
			else
			{
				$result = M("Company") -> where("uid={$_POST['uid']}") -> find();
				if ($result != null)
				{
					//更改企业头像
					if ($result["cplogo"] == null) 
					{
						//已完善资料，但未上传过头像
						$id = M("Company") -> where("uid={$_POST['uid']}") -> setField('cplogo', $data['headimg']);
					}
					else
					{
						//已完善过资料，且上传过头像
// 						$headimgName = substr($result["cplogo"], -36);
						$headimgArray = explode("/", $result["cplogo"]);

						$headimgName = end($headimgArray);
						$delResult = unlink("./Public/Uploads/Headimg/".$headimgName);
						$id = M("Company") -> where("uid={$_POST['uid']}") -> setField('cplogo', $data['headimg']);
					}
				}
				else
				{
					//上传企业头像
					$id = M("Company") -> add($data);
				}
			}
			if ($id == null) {
				
					$array1['result'] = -1;
					$array1['message'] = "图片上传失败！";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
			}else{	
					$array1['result'] = 0;
					$array1['message'] = "头像上传成功！";
					$array1['data'] = $data;
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				
			}
		} 
		//上传个人写真
		public function uploadMirror()
		{
			import('ORG.Cloud.QiniuSDK');
			$key1 = "test.png";
			$Instance = QiniuSDK::getInstance('QiniuRSTransfer');
			$Instance->setScope('cdnimg');
			$upToken=$Instance->Token();
			list($ret, $err) = $Instance->Put($upToken, $key1, "Qiniu 	Storage!");
			if ($err !== null) {
				var_dump($err);
			} else {
				var_dump($ret);
			}
		} 
	}
?>